package com.ruoyi.project.biz.controller;

import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.biz.entity.*;
import com.ruoyi.project.biz.service.FlowInstService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;
import org.springframework.data.domain.Pageable;

import javax.annotation.Resource;
import java.util.List;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * 流程实例表(FlowInst)表控制层
 *
 * @author qingsong
 */
@RestController
@RequestMapping("/flowInst")
public class FlowInstController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private FlowInstService flowInstService;

    @GetMapping("/test")
    public AjaxResult test() {
        return AjaxResult.success("test add");
    }

    @GetMapping("/list")
    public Page<FlowInstDO> listInst(InstQueryEntity queryEntity,
                                  @PageableDefault(value = 10, sort = { "instId" }, direction = Sort.Direction.DESC)Pageable pageable) {
        LoginUser user = getLoginUser();
        queryEntity.setLoginUser(user);
        Page<FlowInstDO> list = flowInstService.getList(queryEntity, pageable);
        return list;
    }

    /**
     * 新增流程
     * @return
     */
    @PostMapping("/addInst")
    public AjaxResult addFlow(InstEntity instEntity) {
        LoginUser user = getLoginUser();
        instEntity.setLoginUser(user);
        return flowInstService.addFlow(instEntity);
    }

    /**
     * 下一环节,step=1 正常流转，step=-1，回退
     * @return
     */
    @PostMapping("/nextStep")
    public AjaxResult nextStep(RecordEntity recordEntity) {
        LoginUser user = getLoginUser();
        recordEntity.setLoginUser(user);
        return flowInstService.nextStep(recordEntity);
    }

    /**
     * 获取流程过程信息
     * @return
     */
    @GetMapping("/getRecordsInfo")
    public TableDataInfo getRecordsInfo(Long instId) {
        LoginUser user = getLoginUser();
        List<FlowRecordDO> list = flowInstService.getRecordsInfo(instId);
        return getDataTable(list);
    }
}
